import { appLogo, storageTokenKey } from '../global/config.js'
import { formatTime } from '../utils/utils.js'

/**
 * 全局异常处理
 */
export const errHandle = function(err) {
	//console.error(err)
	try {
		if(err.code === 10002 || err.code === 10003) {
			//10002:未注册 10003:未登录,如果做了无感知刷新token的处理，err.code不会等于10003
			
			//console.warn("backPage = " + err.backPage + ", backType = " + err.backType)
			let pages = getCurrentPages()
			let currentPage = null
			if(pages && pages.length > 0) {
				currentPage = pages[pages.length - 1]
			}
			
			let isRegister = (err.code === 10002)
			// console.log(currentPage + '----' + currentPage.route);
			if(currentPage && currentPage.route === 'pages/author-page/author-page') {
				let options = currentPage ? (currentPage.options || {}):{}
				let isRegisterFromOptions = JSON.parse(options.isRegister || "false")
				if(!(isRegisterFromOptions ^ isRegister)) {
					//1.当前已经是注册页面了(isRegisterFromOptions = true)，后续又需要跳转注册页面(isRegister = true)
					//2.当前已经是登录页面了(isRegisterFromOptions = false)，后续又需要跳转登录页面(isRegister = false)
					return
				}
			}
			
			let query = ""
			if(err && err.backPageQuery) {
				query = encodeURIComponent(err.backPageQuery)
			}
			let urlInfo = "/pages/author-page/author-page?backPage=" + encodeURIComponent(err.backPage) + "&backType=" + err.backType + "&isRegister=" + isRegister + "&backPageQuery=" + query
			if(err.backType === 'redirect') {
				uni.redirectTo({
					url: urlInfo
				})
			} else {
				uni.navigateTo({
					url: urlInfo
				})
			}
		} else if(err.code === 10004) {
			let pages = getCurrentPages()
			let currentPage = null
			if(pages && pages.length > 0) {
				currentPage = pages[pages.length - 1]
			}
			
			if(currentPage && currentPage.route === 'pages/select-company/select-company') {
				return
			}
			
			let query = ""
			if(err && err.backPageQuery) {
				query = encodeURIComponent(err.backPageQuery)
			}
			
			let urlInfo = "/pages/select-company/select-company?backPage=" + encodeURIComponent(err.backPage) + "&backType=" + err.backType + "&backPageQuery=" + query
			if(err.backType === 'redirect') {
				uni.redirectTo({
					url: urlInfo
				})
			} else {
				uni.navigateTo({
					url: urlInfo
				})
			}
		} else if(err.code === 10005) {
			
			let pages = getCurrentPages()
			let currentPage = null
			if(pages && pages.length > 0) {
				currentPage = pages[pages.length - 1]
			}
			
			if(currentPage && currentPage.route === 'pages/advertisement-page/advertisement-page') {
				return
			}
			
			let query = ""
			if(err && err.backPageQuery) {
				query = encodeURIComponent(err.backPageQuery)
			}
			
			let urlInfo = "/pages/advertisement-page/advertisement-page?backPage=" + encodeURIComponent(err.backPage) + "&backType=" + err.backType + "&backPageQuery=" + query
			if(err.backType === 'redirect') {
				uni.redirectTo({
					url: urlInfo
				})
			} else {
				uni.navigateTo({
					url: urlInfo
				})
			}
		} else {
			uni.showToast({
				title: err.message,
				type: "error", 
				complete: () => {
					
				}
			}).catch((err) => {})
			// #ifdef MP-WEIXIN
			if(err.requestInfo) {
				const logger = wx.getRealtimeLogManager()
				let errDebugInfo = {
					reportTime: formatTime(new Date()),
					err: err.message,
					deviceInfo: uni.getDeviceInfo()
				}
				errDebugInfo["requestInfo"] = err.requestInfo
				//只会上传网络请求层的异常
				//项目上线后可打开,上报的错误日志可在微信小程序后台的开发管理 -> 运维中心 -> 实时日志中查看
				//logger.error("error", errDebugInfo)
			}
			// #endif
		}
	} catch(e) {}
	finally {
		uni.hideLoading()
	}
}

export const onUnhandledRejection = function(event) {
	return event.promise
}